package org.douxiaoban.client.adapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.douxiaoban.client.R;
import org.douxiaoban.client.activity.DouGroupOneTopicAct;
import org.douxiaoban.client.api.DoubanApi;
import org.douxiaoban.client.app.App;
import org.douxiaoban.data.DouGroupTopicData;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * 新版豆瓣小组的首页－－
 * ps：刚写好的正则－就改版－这是人品差呢？还是人品好？
 * @author 6a209
 * 4:36:12 PM May 13, 2012
 */
public class DouGroupNewestAdapter extends BaseAdapter{

	private LayoutInflater mInflater;
	public ArrayList<DouGroupTopicData> mListData;
	private Context mCtx;
	private Map<String, Bitmap> mUrlBitmap;
	
	public Handler mHandler = new Handler(){
		@Override
		public void handleMessage(Message msg){
			Bundle bundle = msg.getData();
			String str = bundle.getString(DoubanApi.KEY_URL);
			Bitmap bitmap = (Bitmap)msg.obj;
			mUrlBitmap.put(str, bitmap);
			notifyDataSetChanged();
		}
	};
	
	public DouGroupNewestAdapter(Context ctx){
		mCtx = ctx;
		mInflater = LayoutInflater.from(ctx);
		mListData = new ArrayList<DouGroupTopicData>();
		mUrlBitmap = new HashMap<String, Bitmap>();
	}
	
	public void setData(ArrayList<DouGroupTopicData> listData){
		mListData = listData;
	}
	
	public void recycle(){
		for(String key : mUrlBitmap.keySet()){
			mUrlBitmap.get(key).recycle();
		}
		mUrlBitmap.clear();
	}
	
	@Override
	public int getCount() {
		return mListData.size();
	}

	@Override
	public Object getItem(int position) {
		return mListData.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		DouGroupTopicData itemData = mListData.get(position);
		if(itemData.mGroupIcon != null && itemData.mGroupIcon.length() > 0){
			//为小组信息
			convertView = inflateGroupInfo(convertView, itemData);
		}else{
			//为话题－－
			convertView = inflateTopicInfo(convertView, itemData);
		}
		return convertView;
	}
	
	private View inflateGroupInfo(View view, DouGroupTopicData data){
		GroupInfoHandleView handleView = new GroupInfoHandleView();
//		if(null == view){
			view = mInflater.inflate(R.layout.group_newest_group_info, null);
			handleView.mIvGroupIcon =
					(ImageView)view.findViewById(R.id.newest_group_info_icon);
			handleView.mTvGroupName = 
					(TextView)view.findViewById(R.id.newest_group_info_name);
//		}else{
//			handleView = (GroupInfoHandleView)view.getTag();
//		}
		handleView.mTvGroupName.setText(data.mGroupName);
		Bitmap bitmap = mUrlBitmap.get(data.mGroupIcon);
		if(null == bitmap){
			App.getDouanApiInstance().reqBitmap(mHandler, data.mGroupIcon);
		}else{
			handleView.mIvGroupIcon.setImageBitmap(bitmap);
		}
//		view.setTag(handleView);
		return view;
	}
	
	private View inflateTopicInfo(View view, final DouGroupTopicData data){
		GroupTopicHandleView handleView = new GroupTopicHandleView();
//		if(null == view){
			view = mInflater.inflate(R.layout.group_newest_topic, null);
			handleView.mTvTopicTitle = 
					(TextView)view.findViewById(R.id.newest_topic_title);
			handleView.mTvReplyCount = 
					(TextView)view.findViewById(R.id.newest_reply_count);
			handleView.mTvTime = 
					(TextView)view.findViewById(R.id.newest_time);
//		}else{
//			handleView = (GroupTopicHandleView)view.getTag();
//		}
		handleView.mTvReplyCount.setText(data.mReplyCount);
		handleView.mTvTime.setText(data.mLastReplyTime);
		handleView.mTvTopicTitle.setText(data.mTitile);
		view.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(mCtx, DouGroupOneTopicAct.class);
				intent.putExtra(DouGroupOneTopicAct.URL_KEY, data.mTopicLink);
				intent.putExtra(DouGroupOneTopicAct.GROUP_NAME_KEY, data.mGroupName);
				intent.putExtra(DouGroupOneTopicAct.GROUP_LINK, data.mGroupLink);
				mCtx.startActivity(intent);
			}
		});
//		view.setTag(handleView);
		return view;
	}
	
	private class GroupInfoHandleView{
		public ImageView mIvGroupIcon;
		public TextView mTvGroupName;
	}
	
	private class GroupTopicHandleView{
		public TextView mTvTopicTitle;
		public TextView mTvReplyCount;
		public TextView mTvTime;
	}
	
	
}